package org.epam.szhuk.greenway.listener;


import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.util.Log;

public class GeoUpdateListener implements LocationListener , GpsStatus.Listener {
	public static final String LOCATION_KEY = "location";
	public static final String LOC_CHGD_MSG_KEY = "loc_changed";
	public static final String STTS_CHGD_MSG_KEY = "status_changed";
	public static final String STATUS_KEY = "status";
	//private static final String TAG = "GeoUpdateListener";
	public static final String GPS_STTS_CHGD_MSG_KEY = "gps_status_changed";
	
	private static GeoUpdateListener mGeoUpdateHandlerInstance;
	private Handler mHandler;
	private long mLastLocationMillis;
	private boolean isGPSFix;
	private Location mLastLocation;
	
	private GeoUpdateListener(){}
	
	public static GeoUpdateListener getInstance()
	{
		if(mGeoUpdateHandlerInstance==null)
			mGeoUpdateHandlerInstance=new GeoUpdateListener();
		return mGeoUpdateHandlerInstance;
	}
	
	public void registerHandler(Handler handler)
	{
		this.mHandler = handler;
	}

	public void onLocationChanged(Location location) {
		Message msg = new Message();
		Bundle data  = new Bundle();
		
		data.putString("msg", LOC_CHGD_MSG_KEY);
		data.putParcelable(LOCATION_KEY, location);
		msg.setData(data);
		mHandler.sendMessage(msg);
		mLastLocationMillis=SystemClock.elapsedRealtime();
		mLastLocation = location;
	}

	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	public void onStatusChanged(String provider, int status, Bundle extras) {
		Message msg = new Message();
		Bundle data  = new Bundle();
		
		data.putString("msg", STTS_CHGD_MSG_KEY);
		data.putInt(STATUS_KEY, status);
		msg.setData(data);
		mHandler.sendMessage(msg);
	}

	    public void onGpsStatusChanged(int event) {
	    	//Log.d(TAG, "onGpsStatusChanged");
			switch (event) {
	            case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
				if (mLastLocation != null)
	                    isGPSFix = (SystemClock.elapsedRealtime() - mLastLocationMillis) < 3000;

	                if (isGPSFix) { // A fix has been acquired.
	                    // Do something.
	                } else { // The fix has been lost.
	                    // Do something.
	                }

	                break;
	            case GpsStatus.GPS_EVENT_FIRST_FIX:
	                // Do something.
	                isGPSFix = true;

	                break;
	        }
			Message msg = new Message();
			Bundle data  = new Bundle();
			
			data.putString("msg", GPS_STTS_CHGD_MSG_KEY);
			data.putBoolean(STATUS_KEY, isGPSFix);
			msg.setData(data);
			mHandler.sendMessage(msg);

	}

}
